import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:yh_core_flutter_project/project/modules/discover/pages/discover_page.dart';
import 'package:yh_core_flutter_project/project/modules/home/pages/home_page.dart';
import 'package:yh_core_flutter_project/project/modules/message/pages/message_page.dart';
import 'package:yh_core_flutter_project/project/modules/mine/pages/mine_page.dart';
import '../../common/constant/colors.dart';
import '../../common/constant/icon.dart';


class MainTabPage extends StatefulWidget {
  const MainTabPage({super.key});

  @override
  State<MainTabPage> createState() => _MainTabPageState();
}

class _MainTabPageState extends State<MainTabPage> {
  int _currentIndex = 0;
  
  final List<Widget> _pages = [
    const HomePage(),
    const DiscoverPage(),
    const MessagePage(),
    const MinePage(),
  ];
  
  List<BottomNavigationBarItem> _buildTabItems() {
    return [
      BottomNavigationBarItem(
        icon: _buildTabIcon(AppIcon.icTabHomeN, false, 0),
        activeIcon: _buildTabIcon(AppIcon.icTabHomeS, true, 0),
        label: '主页',
      ),
      BottomNavigationBarItem(
        icon: _buildTabIcon(AppIcon.icTabAmazonN, false, 1),
        activeIcon: _buildTabIcon(AppIcon.icTabAmazonS, true, 1),
        label: '发现',
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Icons.message_outlined, 
          size: 22,
          color: _currentIndex == 2 ? AppColor.textPrimary : AppColor.textSecondary,
        ),
        activeIcon: Icon(
          Icons.message, 
          size: 22,
          color: AppColor.textPrimary,
        ),
        label: '消息',
      ),
      BottomNavigationBarItem(
        icon: _buildTabIcon(AppIcon.icTabMineN, false, 3),
        activeIcon: _buildTabIcon(AppIcon.icTabMineS, true, 3),
        label: '我的',
      ),
    ];
  }

  Widget _buildTabIcon(String iconPath, bool isActive, int index) {
    final isSelected = _currentIndex == index;
    // 使用更深的颜色作为选中状态
    final color = isActive || isSelected ? AppColor.textPrimary : AppColor.textSecondary;
    
    return SvgPicture.asset(
      iconPath,
      width: 22,
      height: 22,
      colorFilter: ColorFilter.mode(color, BlendMode.srcIn),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.scaffoldBackground,
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          border: Border(
            top: BorderSide(
              color: AppColor.lineColor,
              width: 0.5,
            ),
          ),
        ),
        child: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          selectedItemColor: AppColor.textPrimary,
          unselectedItemColor: AppColor.textSecondary,
          selectedLabelStyle: TextStyle(fontSize: 11, fontWeight: FontWeight.w500),
          unselectedLabelStyle: TextStyle(fontSize: 11, fontWeight: FontWeight.normal),
          iconSize: 22,
          backgroundColor: Colors.transparent,
          elevation: 0,
          items: _buildTabItems(),
        ),
      ),
    );
  }
} 